#include "dominion.h"
#include "dominion_helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rngs.h"
#include <math.h>


//Random test adventurer
int randomtestadventurer(){

	int i, test_val, test_val2, h, num_of_test, test_num, success_count;
	int t_count_hand_int, t_count_hand_aft, t_count_deck_int, t_count_deck_aft, t_count_total;
	int deck_fail_count, total_treasure_fail;
	int seed = 1000;
	int numPlayer = 2;
	int testhand_count1, testhand_count2;
	int testbuys1, testbuys2;
	int handPos, choice1, choice2, choice3;
	int bonus;
	int k[10] = {adventurer, great_hall, feast, gardens, mine, remodel, smithy, council_room, baron, village};
	struct gameState G;
	int maxHandCount = 10;
	SelectStream(4);
	PutSeed(5);
	srand(8);
	num_of_test = 500000;


	

	memset(&G, 0, sizeof(struct gameState));
	test_val = initializeGame(numPlayer, k, seed, &G);

	//if (test_val == -1){
	//	printf("initializeGame failed\n");
	//}
	//else{
	//	printf("initializeGame Passed\n");
	//}
	//G.handCount[0] = 9;
	//drawCard(0, &G);
	//drawCard(0, &G);
	//drawCard(0, &G);
	//G.hand[0][4] = 14;
	test_val = numHandCards(&G);
	//printf("number of cards in hand is %d full deckcount is %d\n", numHandCards(&G), fullDeckCount(0, 0, &G));
	//for(h = 0; h < test_val; h++){
	//	printf("the card is at %d cost is %d\n", handCard(h, &G), getCost(G.hand[0][h]));
	//}
	//updateCoins(0, &G, 0);
	//test_val2 = handCard(2, &G);
	//test_val2 = buyCard(14, &G);
	
	//gainCard(14, &G, 2, 0);
	//printf("testing something %d\n", test_val);




	testhand_count1 = numHandCards(&G);
	test_val = G.handCount[0];
	success_count = 0;
	deck_fail_count = 0;
	total_treasure_fail = 0;
	
	for(test_num = 1; test_num <= num_of_test; test_num++){
		srand(test_num);
		t_count_deck_int = 0;
		t_count_deck_aft = 0;
		t_count_hand_int = 0;
		t_count_hand_aft = 0;
		G.handCount[0] = testhand_count1;
		G.deckCount[0] = 50;
		G.discardCount[0] = 0;
		for (h = 0; h < testhand_count1; h++){
			G.hand[0][h] = rand() % 26;
			if(G.hand[0][h] == 4 || G.hand[0][h] == 5 || G.hand[0][h] == 6)
				t_count_hand_int += 1;
		}
		for (h = 0; h < G.deckCount[0]; h++){
			G.deck[0][h] = rand() % 26;
			if(G.deck[0][h] == 4 || G.deck[0][h] == 5 || G.deck[0][h] == 6)
				t_count_deck_int += 1;
		}
		t_count_total = t_count_hand_int + t_count_deck_int;
		//printf("Treasures. Hand %d, Deck %d\n", t_count_hand_int, t_count_deck_int);
		cardEffect(k[0], choice1, choice2, choice3, &G, handPos, &bonus);
		if(G.hand[0][test_val] == 4 || G.hand[0][test_val] == 5 || G.hand[0][test_val] == 6){
			if(G.hand[0][test_val+1] == 4 || G.hand[0][test_val+1] == 5 || G.hand[0][test_val+1] == 6){
				//printf("Test %d of %d: Success Card 1: %d, Card 2: %d, HandCount: %d, Cards Discarded: %d\n", test_num, num_of_test, G.hand[0][test_val], G.hand[0][test_val+1], G.handCount[0], G.discardCount[0]);
				success_count += 1;
			}
			else{
				if(t_count_deck_int < 2){
					printf("Test %d of %d: Failed Deck Treasure Count: %d\n", test_num, num_of_test, t_count_deck_int);
					deck_fail_count += 1;
				}
				else if(t_count_total <= 2){
					printf("Test %d of %d: Failed Total Treasure Count: %d\n", test_num, num_of_test, t_count_total);
					total_treasure_fail += 1;
				}

			}
		}
		else{
			if(t_count_deck_int < 2){
				printf("Test %d of %d: Failed Deck Treasure Count: %d\n", test_num, num_of_test, t_count_deck_int);
				deck_fail_count += 1;
			}
			else if(t_count_total <= 2){
				printf("Test %d of %d: Failed Total Treasure Count: %d\n", test_num, num_of_test, t_count_total);
				total_treasure_fail += 1;
			}
		}
	}
	if(success_count > 0)
		printf("%d out of %d successful\n", success_count, num_of_test);
	if(deck_fail_count > 0)
		printf("%d out of %d failed due to lack of treasures in deck\n", deck_fail_count, num_of_test);
	if(total_treasure_fail > 0)
		printf("%d out of %d failed due to lack of treasures in hand\n", total_treasure_fail, num_of_test);



	return 0;
}

int main(int argc, char *argv[]){

	randomtestadventurer();
}